import axios from "axios";
const Axios =axios.create({
    baseURL:"http://101.35.142.7:4231",
    withCredentials:true,
    headers:{
      "Content-type":["application/json"],
    },
    timeout:6000 //超时时间
})
Axios.interceptors.request.use(
    config => {
      if (config.method === "post" || config.method === "put") {
        // 格式化post参数为json再发送
        // config.data = JSON.stringify(config.data)
        // console.log(config.data);
      }
      // 将携带token进行请求
      let token=window.localStorage.getItem("token")
      if (token) {
        config.headers['Authorization'] = token;
      }
      return config;
    },
    error => {
      console.log("===发送请求拦截器报错===")
      console.log(error);
      console.log("===end===");
      Promise.reject(error);
      if(error.message.includes("timeout")){
        console.log("请求超时");
      }
    }
  );
  Axios.interceptors.response.use(
    res => {
      if (res.data.status == 403) {
        localStorage.removeItem("token");
        sessionStorage.removeItem("token");

      } else if (res.data.code == -1) {
        store.commit("setIslogin", false);
        store.commit("setUname", "");
        //alert(res.data.msg+" 请先登录 !");
      } else if (res.data.token) {
        // 登录成功
        // 保存密码功能
        if (res.data.remember === "true") {
          store.commit("setUname", res.data.data.uname);
          store.commit("setIslogin", true);
          localStorage.setItem("token", res.data.token);
        } else {
          store.commit("setUname", res.data.data.uname);
          store.commit("setIslogin", true);
          sessionStorage.setItem("token", res.data.token);
        }
      }
      // 将后续res都替换为res.data
      res=res.data
      return res;
    },
    error => {
  
    }
  )
export {
  Axios as axios
}